package com.ruoyi.mes.material.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mes.systembasic.domain.SmesProduceSequenceResult;
import com.ruoyi.mes.systembasic.domain.SmesSequenceProduce;
import com.ruoyi.mes.systembasic.mapper.SmesSequenceProduceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.material.mapper.SmesMaterialOutInBillMapper;
import com.ruoyi.mes.material.domain.SmesMaterialOutInBill;
import com.ruoyi.mes.material.service.ISmesMaterialOutInBillService;

/**
 * 原料出入库订单管理Service业务层处理
 *
 * @author simpsense
 * @date 2020-12-15
 */
@Service
public class SmesMaterialOutInBillServiceImpl implements ISmesMaterialOutInBillService
{
    @Autowired
    private SmesMaterialOutInBillMapper smesMaterialOutInBillMapper;

    @Autowired
    private SmesSequenceProduceMapper smesSequenceProduceMapper;

    /**
     * 查询原料出入库订单管理
     *
     * @param outInBillNo 原料出入库订单管理ID
     * @return 原料出入库订单管理
     */
    @Override
    public SmesMaterialOutInBill selectSmesMaterialOutInBillById(String outInBillNo)
    {
        return smesMaterialOutInBillMapper.selectSmesMaterialOutInBillById(outInBillNo);
    }

    /**
     * 查询原料出入库订单管理列表
     *
     * @param smesMaterialOutInBill 原料出入库订单管理
     * @return 原料出入库订单管理
     */
    @Override
    public List<SmesMaterialOutInBill> selectSmesMaterialOutInBillList(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        return smesMaterialOutInBillMapper.selectSmesMaterialOutInBillList(smesMaterialOutInBill);
    }

    /**
     * 新增原料出入库订单管理
     *
     * @param smesMaterialOutInBill 原料出入库订单管理
     * @return 结果
     */
    @Override
    public int insertSmesMaterialInBillOther(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        SmesSequenceProduce smesSequenceProduce=new SmesSequenceProduce();
        smesSequenceProduce.setBillType("原料其他入库");
        smesSequenceProduce.setBillPrefix("MOI");
        smesSequenceProduce.setUserName(SecurityUtils.getUsername());
        SmesProduceSequenceResult geBillNo=smesSequenceProduceMapper.generateSequence(smesSequenceProduce);
        if(geBillNo==null||geBillNo.getCode()==null||!geBillNo.getCode().equals("OK")){
            return 0;
        }
        smesMaterialOutInBill.setOutInBillNo(geBillNo.getMsg());
        smesMaterialOutInBill.setCreateTime(DateUtils.getNowDate());
        smesMaterialOutInBill.setCreateBy(SecurityUtils.getUsername());
        return smesMaterialOutInBillMapper.insertSmesMaterialOutInBill(smesMaterialOutInBill);
    }

    @Override
    public int insertSmesMaterialOutBillOther(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        SmesSequenceProduce smesSequenceProduce=new SmesSequenceProduce();
        smesSequenceProduce.setBillType("原料其他出库");
        smesSequenceProduce.setBillPrefix("MOO");
        smesSequenceProduce.setUserName(SecurityUtils.getUsername());
        SmesProduceSequenceResult geBillNo=smesSequenceProduceMapper.generateSequence(smesSequenceProduce);
        if(geBillNo==null||geBillNo.getCode()==null||!geBillNo.getCode().equals("OK")){
            return 0;
        }
        smesMaterialOutInBill.setOutInBillNo(geBillNo.getMsg());
        smesMaterialOutInBill.setCreateTime(DateUtils.getNowDate());
        smesMaterialOutInBill.setCreateBy(SecurityUtils.getUsername());
        return smesMaterialOutInBillMapper.insertSmesMaterialOutInBill(smesMaterialOutInBill);
    }
    @Override
    public int insertSmesMaterialOutBillNormal(SmesMaterialOutInBill smesMaterialOutInBill) {

        SmesSequenceProduce smesSequenceProduce=new SmesSequenceProduce();
        smesSequenceProduce.setBillType("生产领料");
        smesSequenceProduce.setBillPrefix("MPO");
        smesSequenceProduce.setUserName(SecurityUtils.getUsername());
        SmesProduceSequenceResult geBillNo=smesSequenceProduceMapper.generateSequence(smesSequenceProduce);
        if(geBillNo==null||geBillNo.getCode()==null||!geBillNo.getCode().equals("OK")){
            return 0;
        }
        smesMaterialOutInBill.setOutInBillNo(geBillNo.getMsg());
        smesMaterialOutInBill.setCreateTime(DateUtils.getNowDate());
        smesMaterialOutInBill.setCreateBy(SecurityUtils.getUsername());
        return smesMaterialOutInBillMapper.insertSmesMaterialOutInBill(smesMaterialOutInBill);
    }

    /**
     * 修改原料出入库订单管理
     *
     * @param smesMaterialOutInBill 原料出入库订单管理
     * @return 结果
     */
    @Override
    public int updateSmesMaterialOutInBill(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        smesMaterialOutInBill.setUpdateTime(DateUtils.getNowDate());
        smesMaterialOutInBill.setUpdateBy(SecurityUtils.getUsername());
        return smesMaterialOutInBillMapper.updateSmesMaterialOutInBill(smesMaterialOutInBill);
    }

    /**
     * 批量删除原料出入库订单管理
     *
     * @param outInBillNos 需要删除的原料出入库订单管理ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialOutInBillByIds(String[] outInBillNos)
    {
        return smesMaterialOutInBillMapper.deleteSmesMaterialOutInBillByIds(outInBillNos);
    }

    /**
     * 删除原料出入库订单管理信息
     *
     * @param outInBillNo 原料出入库订单管理ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialOutInBillById(String outInBillNo)
    {
        return smesMaterialOutInBillMapper.deleteSmesMaterialOutInBillById(outInBillNo);
    }
}